<?php 

$docroot = realpath( dirname( __FILE__ ) );

include( "$docroot/config.php");
include( "$docroot/db.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

include( "$docroot/user.php");

//rovat lista
$rovatok = array ();
$db->sql = "SELECT id,nev,slug,parent FROM `rovat` WHERE parent='0' ORDER BY `order`";
$db->query();
while ($row = mysql_fetch_assoc($db->rs)) $rovatok[]=$row;
for($i=0;$i<count($rovatok);$i++) {
	$db->sql = "SELECT id,nev,slug,parent FROM `rovat` WHERE parent='".$rovatok[$i]['id']."' ORDER BY `order`";
	$db->query();
	while ($row = mysql_fetch_assoc($db->rs)) $rovatok[$i]['childs'][]=$row;					
}

//regio lista
$regiok = array ();
$db->sql = "SELECT id,nev,slug,parent FROM `regio` WHERE parent='0' ORDER BY `order`";
$db->query();
while ($row = mysql_fetch_assoc($db->rs)) $regiok[]=$row;
for($i=0;$i<count($regiok);$i++) {
	$db->sql = "SELECT id,nev,slug,parent FROM `regio` WHERE parent='".$regiok[$i]['id']."' ORDER BY `order`";
	$db->query();
	while ($row = mysql_fetch_assoc($db->rs)) $regiok[$i]['childs'][]=$row;					
}

$tegnap = date("Y-m-d", time()-25*60*60);     
$ma     = date("Y-m-d", time());     
$holnap = date("Y-m-d", time()+25*60*60);
$ma     = "'{$ma}'"; 
$holnap = "'{$holnap}'";

//counters		
$db->sql = "SELECT COUNT(id) FROM hirdetes WHERE szponzoralt>0 AND aktiv=1";
$db->query();
$rs = (mysql_fetch_row( $db->rs));
$ct_kiemelt = $rs[0];

$db->sql = "SELECT COUNT(id) FROM hirdetes WHERE ((lastmodified>=$ma AND lastmodified<$holnap) OR szponzoralt>0) AND aktiv=1";
$db->query(); 
$rs = (mysql_fetch_row( $db->rs));
$ct_friss = $rs[0];

$db->sql = "SELECT COUNT(id) FROM hirdetes WHERE aktiv=1";
$db->query();
$rs = (mysql_fetch_row( $db->rs));
$ct_osszes = $rs[0];

$db->sql = "SELECT COUNT(id) FROM hirdetes WHERE DATEDIFF(lejarat,$ma)=0 AND aktiv=1";
$db->query();
$rs = (mysql_fetch_row( $db->rs));
$ct_lejaro = $rs[0];

$db->sql = "SELECT COUNT(id) FROM hirdetes WHERE rovat=50 OR rovat=51 AND aktiv=1";
$db->query();
$rs = (mysql_fetch_row( $db->rs));
$ct_ismerkedes = $rs[0];

//referers
$referer = isset( $_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "";

if (
$referer != "" && 
$referer != "hirdetek.net" && 
$referer != "hirdetek.net/" && 
$referer != "www.hirdetek.net" && 
$referer != "www.hirdetek.net/" 	
) {
	$ref_link = $referer;
	if (0 !== strpos( $ref_link, 'http://')) $ref_link = 'http://' . $ref_link;

	$ref_link = htmlspecialchars( $ref_link);
			
	$db->sql = "UPDATE referers SET ref_num=ref_num+1, ref_link='$ref_link' WHERE ref_url='$referer'";
	$db->query ();      
   
	if (mysql_affected_rows () < 1) {
		
		$db->sql = "INSERT INTO referers SET ref_url='$referer', ref_link='$ref_link', ref_num=1";
		$db->query ();     
	}
}

$db->sql = "SELECT ref_url, ref_link, ref_num
		    FROM referers 
		    WHERE ref_link NOT LIKE '%mail%' AND ref_link NOT LIKE '%google%' AND ref_url NOT LIKE '%hirdetek.net%' 
		    AND ref_url NOT REGEXP '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 
		    AND ref_url NOT LIKE '%vizsla24%' AND ref_url NOT LIKE '%freeweb%' AND ref_url NOT LIKE '%polymeta%' AND ref_url NOT LIKE '%whois.sc%' 
		    AND ref_url NOT LIKE '%ingyenhirdetek%' AND ref_url NOT LIKE '%ok.hu%' AND ref_url NOT LIKE '%bing.com%' AND ref_url NOT LIKE '%wpl.hu%' 
		    AND ref_url NOT LIKE '%startlap.hu%' 
		    AND ref_url NOT LIKE '%search%' AND ref_url NOT LIKE '%whois.domaintools%' AND ref_url NOT LIKE 'szexparti.com' AND ref_url NOT LIKE '%localhost%'
		    AND ref_noDisp = 0 
		    ORDER BY ref_num DESC 
		    LIMIT 0, 10";	   
$db->query (); 

$top_referers = array ();
while( $row = mysql_fetch_assoc( $db->rs)) {  	
	$top_referers[] = $row;
}

$db->sql = "SELECT ref_url, ref_link, ref_num
		    FROM referers 
		    WHERE ref_link NOT LIKE '%mail%' AND ref_link NOT LIKE '%google%' AND ref_url NOT LIKE '%hirdetek.net%' 
		    AND ref_url NOT REGEXP '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 
		    AND ref_url NOT LIKE '%vizsla24%' AND ref_url NOT LIKE '%freeweb%' AND ref_url NOT LIKE '%polymeta%' AND ref_url NOT LIKE '%whois.sc%' 
		    AND ref_url NOT LIKE '%ingyenhirdetek%' AND ref_url NOT LIKE '%ok.hu%' AND ref_url NOT LIKE '%bing.com%' AND ref_url NOT LIKE '%wpl.hu%' 
		    AND ref_url NOT LIKE '%startlap.hu%' 
		    AND ref_url NOT LIKE '%search%' AND ref_url NOT LIKE '%whois.domaintools%' AND ref_url NOT LIKE 'szexparti.com' AND ref_url NOT LIKE '%localhost%'
		    AND ref_noDisp = 0 
			AND ref_num>5
		    ORDER BY ref_id DESC,ref_num DESC
		    LIMIT 0, 10";	   
$db->query (); 

$new_referers = array ();
while( $row = mysql_fetch_assoc( $db->rs)) {  	
	$new_referers[] = $row;
}

//debug ($referers);

// loggolas es on-line felhasznalok szamolasa

$db->sql = "UPDATE log SET log_userId=".User::get_id().", log_userIP='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."', log_activity='".date ( "Y-m-d H:i:s", time ())."' WHERE log_sessId='".session_id ()."'";
$db->query();     

if (mysql_affected_rows() < 1) {
$db->sql = "INSERT INTO log SET log_sessId='".session_id ()."', log_userId=".User::get_id().", log_userIP='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."', log_activity='".date ( "Y-m-d H:i:s", time ())."'";
$db->query();     
}   

$db->sql = "SELECT log_id FROM log WHERE log_activity>'".date( "Y-m-d H:i:s", time () - 30 * 60)."'";
$db->query();

$average_online_users = mysql_num_rows( $db->rs);

//$db->sql = "SELECT log_id FROM log WHERE log_activity>'".date( "Y-m-d H:i:s", time () - 30 * 60)."' AND log_userId > 0";
//$db->query();
//$online_registered_users = mysql_num_rows( $db->rs);  
//$online_guest_users = $average_online_users - $online_registered_users;
   
// linkcsere partnerek
/*
$db->sql = "SELECT * 
            FROM linkcsere 
            WHERE lcs_enabled=1 AND lcs_banned=0 
            ORDER BY lcs_vip DESC, lcs_pageRank DESC, lcs_id DESC 
            LIMIT 31";
*/

$db->sql = "SELECT l.*,
			(SELECT SUM(r.ref_num) FROM referers r WHERE r.ref_link = l.lcs_linkhely) AS refcount
			FROM linkcsere l
			ORDER BY l.lcs_vip DESC, refcount DESC, l.lcs_regDate";
$db->query(); 

$linkpartners = array();

while ($row = mysql_fetch_array( $db->rs)) {
	$linkpartners[] = $row;
}  	
		
function getHostFromUrl ($url) {
	
	$url = preg_replace( '/^[\\w]{0,10}?:\/\//', '', $url);

	$url = str_replace( "www.", "", $url);

	$url = rtrim( $url, "/");

	$url = substr( $url, 0, 20);

	$url = str_replace( "?", "", $url);
		
	while(( $nurl = dirname( $url)) != $url && $nurl != '.') {
		$url = $nurl;
	}

	return $url;
} 

function slug ($str, $delimiter='-') {
	
	$replace = array ("'");	//spec characters need to be replace to a delimiter  e.g. I'll be back -> i-ll-be-back
	
	$str = str_replace ($replace, ' ', $str);

	$clean = iconv ('UTF-8', 'ASCII//TRANSLIT', $str);
	$clean = preg_replace ("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
	$clean = strtolower (trim ($clean, $delimiter));
	$clean = preg_replace ("/[\/_|+ -]+/", $delimiter, $clean);

	return $clean;
}

function str_to_currency ($str) {
	   
   $curr = $str[strlen( $str)-1];
      
   $j = 1;
   
   for ($i = strlen( $str) - 2; $i >= 0; $i--){
      if (($i - strlen( $str) - 2) % 3 == 0) {
         $curr[$j] = ".";
         $j++;
      }
      
      $curr[$j] = $str[$i];
      $j++;  
   }
   
   $rcurr = $curr;

   for ($i = 1; $i <= strlen( $curr); $i++) $rcurr [$i-1] = $curr[strlen( $curr) - $i];
   
   return $rcurr." Ft";
}

function debug ($var, $cap='') {
	return;	
	if ($cap != '') print $cap.'<br />';
	print    '<pre>';
	print_r ($var);
	print    '</pre>';
	print    '<br /><br />';
}

?>
